package com.tx.collections.list;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 */
public class CopyOnWriteArrayListDemo {
    public static void main(String[] args) {
//        test1();

        testFor();
    }

    private static void testFor() {
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
        list.add("hello");
        list.add("world");
        list.add("mercury");
        //当前遍历的list是原容器，所以当开启线程去clear时，还是可以打印出字符串
        for (String str : list) {
            new Thread(()->{ list.clear(); }).start();
            System.out.println(str);
        }

        //这里的结果可能为[],也可能为原容器的值
        //为[]:代表已经clear了
        //为原容器的值：代表clear还没有执行setArray操作
        System.out.println(list);
    }

    private static void test1() {
        CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList();
        copyOnWriteArrayList.add("111");

        Object o = copyOnWriteArrayList.get(0);
        System.out.println(o);

        Object remove = copyOnWriteArrayList.remove(0);
    }
}